<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/19
 * Time: 11:19
 * 异步mysql
 */
class AysncMysql
{
    public $db = null;
    public $config = [];
    public function __construct()
    {
        $this->db = new swoole_mysql();
        $this->config = [
            'host' => '120.24.226.7',
            'user' => 'root',
            'password' => 'goodboy!@#',
            'port' => '3306',
            'database' => 'test',
            'charset' => 'utf8'
        ];
    }

    public function execute($id, $username)
    {
        $this->db->connect($this->config, function($db, $result) use($id,$username){
            if($result === false){
                var_dump($db->connect_error);
            }
            echo "connected" . PHP_EOL;
            //$sql = "SELECT * FROM test WHERE id=" . $id;
            $sql = "UPDATE test SET `name`='{$username}' WHERE id=" . $id;
            $db->query($sql, function($db, $result){
                if($result === false){
                    var_dump($db->error);
                }elseif($result === true){ //add, update,delete
                    var_dump($db->affected_rows) . PHP_EOL;
                }else{//select的结果集
                    print_r($result) . PHP_EOL;
                }
                $db->close();
            });
        });
        return true;
    }
}
$obj = new AysncMysql();
$rs = $obj->execute(1, 'guixianfeng123');
var_dump($rs) . PHP_EOL;
echo 'start' .PHP_EOL;
echo "-----------------------------下面是链接异步回调里面的内容" . PHP_EOL;